<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备类型" prop="sbType">
        <el-select
          v-model="queryParams.sbType"
          placeholder="请选择"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in sbTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="安装位置" prop="sbInstallation">
        <el-input
          v-model="queryParams.sbInstallation"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂家" prop="sbManufacturer">
        <el-input
          v-model="queryParams.sbManufacturer"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:rainDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:rainDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:rainDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="rainDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark" width="150"/>
      <el-table-column
        label="设备类型"
        align="center"
        prop="sbType"
        width="150"
        :formatter="sbTypeFormat"
      />
      <el-table-column
        label="设备安装位置"
        align="center"
        width="180"
        prop="sbInstallation"
      />
      <el-table-column
        label="安装日期"
        align="center"
        prop="sbazTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sbazTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产厂家" align="center" prop="sbManufacturer"  width="150"/>
      <el-table-column
        label="检验日期"
        align="center"
        prop="sbjyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sbjyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="坐标X" align="center" prop="sbX" width="150"/>
      <el-table-column label="坐标Y" align="center" prop="sbY" width="150"/>
      <el-table-column label="坐标Z" align="center" prop="sbZ" width="150"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:rainDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:rainDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改降水量监测基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="煤矿编码" prop="mineCode">
          <el-input v-model="form.mineCode" placeholder="请输入煤矿编码"/>
        </el-form-item>
        <el-form-item label="煤矿名称" prop="mineName">
          <el-input v-model="form.mineName" placeholder="请输入煤矿名称"/>
        </el-form-item>-->
        <el-form-item label="原编码" prop="cdCode">
          <el-input v-model="form.cdCode" placeholder="请输入原测点编码" />
        </el-form-item>
        <el-form-item label="设备类型" prop="sbType">
          <el-select
            v-model="form.sbType"
            placeholder="请选择设备类型"
            style="width:100%"
          >
            <el-option
              v-for="dict in sbTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备安装位置" prop="sbInstallation">
          <el-input
            v-model="form.sbInstallation"
            placeholder="请输入设备安装位置"
          />
        </el-form-item>
        <el-form-item label="安装日期" prop="sbazTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.sbazTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择安装日期"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产厂家" prop="sbManufacturer">
          <el-input
            v-model="form.sbManufacturer"
            placeholder="请输入生产厂家"
          />
        </el-form-item>
        <el-form-item label="检验日期" prop="sbjyTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.sbjyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择检验日期"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="坐标X" prop="sbX">
          <el-input v-model="form.sbX" placeholder="请输入坐标X" />
        </el-form-item>
        <el-form-item label="坐标Y" prop="sbY">
          <el-input v-model="form.sbY" placeholder="请输入坐标Y" />
        </el-form-item>
        <el-form-item label="坐标Z" prop="sbZ">
          <el-input v-model="form.sbZ" placeholder="请输入坐标Z" />
        </el-form-item>
        <!--<el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRainDefine,
  getRainDefine,
  delRainDefine,
  addRainDefine,
  updateRainDefine
} from "@/api/define/rainDefine";

export default {
  name: "RainDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 降水量监测基础信息表格数据
      rainDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设备类型字典
      sbTypeOptions: [],
      // 安装日期时间范围
      daterangeSbazTime: [],
      // 检验日期时间范围
      daterangeSbjyTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cdCode: null,

        sbType: null,

        sbInstallation: null,

        sbazTime: null,

        sbManufacturer: null,

        sbjyTime: null,

        sbX: null,

        sbY: null,

        sbZ: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        sbType: [
          { required: true, message: "设备类型不能为空", trigger: "change" }
        ],
        sbInstallation: [
          { required: true, message: "设备安装位置不能为空", trigger: "blur" }
        ],
        sbazTime: [
          { required: true, message: "安装日期不能为空", trigger: "blur" }
        ],
        sbManufacturer: [
          { required: true, message: "生产厂家不能为空", trigger: "blur" }
        ],
        sbjyTime: [
          { required: true, message: "检验日期不能为空", trigger: "blur" }
        ],
        sbX: [{ required: true, message: "坐标X不能为空", trigger: "blur" }],
        sbY: [{ required: true, message: "坐标Y不能为空", trigger: "blur" }],
        sbZ: [{ required: true, message: "坐标Z不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sb_type").then(response => {
      this.sbTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询降水量监测基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSbazTime && "" != this.daterangeSbazTime) {
        this.queryParams.params["beginSbazTime"] = this.daterangeSbazTime[0];
        this.queryParams.params["endSbazTime"] = this.daterangeSbazTime[1];
      }
      if (null != this.daterangeSbjyTime && "" != this.daterangeSbjyTime) {
        this.queryParams.params["beginSbjyTime"] = this.daterangeSbjyTime[0];
        this.queryParams.params["endSbjyTime"] = this.daterangeSbjyTime[1];
      }
      listRainDefine(this.queryParams).then(response => {
        this.rainDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 设备类型字典翻译
    sbTypeFormat(row, column) {
      return this.selectDictLabel(this.sbTypeOptions, row.sbType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        sbType: null,

        sbInstallation: null,

        sbazTime: null,

        sbManufacturer: null,

        sbjyTime: null,

        sbX: null,

        sbY: null,

        sbZ: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSbazTime = [];
      this.daterangeSbjyTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加降水量监测基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRainDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改降水量监测基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRainDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRainDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delRainDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
